#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

#include "relay.h"

struct relaydev*
        relaydev_construct(
                void)
{
	int gpionum[6] = {48, 49, 50, 51, 52, 53};
	struct relaydev *dev;
	int i;
		
	dev = calloc(1, sizeof(struct relaydev));
	if (NULL == dev) {
		perror("calloc relaydev");
		return NULL;
	}

	for (i = 0; i < 6; i++) { 
		dev->gpiodev[i] = gpiodev_construct(gpionum[i]);
		if (NULL == dev->gpiodev[i]) {
			printf("gpiodev_construct %d error\n", i);
			return NULL;
		}
		gpiodev_export(dev->gpiodev[i]);
		gpiodev_setdir(dev->gpiodev[i], 0);
		gpiodev_setval(dev->gpiodev[i], 1);
	}

	return dev;
}


void
        relaydev_deconstruct(
        	struct relaydev *dev)
{
	int i;

	for (i = 0; i < 6; i++)
		gpiodev_deconstruct(dev->gpiodev[i]);
	free(dev);
}


void
        relaydev_on(
                struct relaydev *dev, int num)
{

	gpiodev_setval(dev->gpiodev[num], 0);
}

void
        relaydev_off(
                struct relaydev *dev, int num)
{
	gpiodev_setval(dev->gpiodev[num], 1);
}















